#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <sstream>
#include "../DB.h"

using namespace std;

const char FILENAME_EN[23] = "MemberAccount_EN.txt";
const char FILENAME_DE[23] = "MemberAccount_DE.txt";
const int MIN_PASSWORD_LENGTH = 5;
const int MAX_PASSWORD_LENGTH = 15;
const int MIN_POINTS = 0;
const int MAX_POINTS = 99999;
const int MAX_REVIEW = 5;
const int NO_OF_MEMBERS = 50;
// originally 391
const int NO_OF_HOTELS = 15; 
const string NAME[363] = {"Yap Sae-hwa Daniel","Neo Yew Chong","Wong Meng Ern Philip","Habib Nur S/o Basheer","Neal Kok Zhi An","Isaac Tan Wei Long","Mantej Singh","Gideon Phong Wei Sheng","John Robinson Tan","Khalis Adil B Abdul Kadir","Kung Htet San","Ammar Waleed","Keefe Ye Kang","Muhammad Nurhan Akid B Hadely","Austin Tang Chee Onn","Chong Wan Hong","Yow Shane","Muhammad Zafri B Zazali","Nickson Ng Jia Rong","Putera Muhammad Ariffin B M","Nabil Asyraf B Jumain","Mohammad Zikri Alif B M M","Mohammad Faizal B Nor Azlan","Mohammad Saiful Bahrain B S","Muhammad Taufiq B Azmi","Alan Poh Chee Pang","Muhammad Elfy B Noraziz","Arfian Jamal","Daniel Lim","Soh Khai","Eugene Wong Joon Hao","Md Iqbal","Teoh Jin Jia","Harith Syahmi","Zhao Hui","Andy Tay Yao Wei","Wong Jia Kai","Kelvin Lee Kay Heng","Chu Meng Hong","Nicky Wong Yong Xin","Mohammed Al Shafirin Khan","Chiew Ming Hui","Samuel Lim Gin Yew","Muhammad Taufiq B Abdul Rahman","Muhammad Danial B Mohamad Azmi","Lim Wei Cong","Christopher John Cabasisi C","Wu Cheng Pang","Lim Zheng Long","Chia Ginray","Seah Wei Heng","Yeo Zi Hao","Yu Hui","Toh Joshua","Lim Zhen Guang","Jim Ang Wei Rong","Muhammed Musaib S/o E","Abdul Razak B Mohd S","Gasper Tan Zhuo Sze","Lim Bing De","Giovanni Felix Santiago","Ong Jia Jun Jerome","Jacob Ashley Khairudin Hai Jie","Seah Song Yu","Yeow Yong Sheng","Wong Man Cong","Nicholas Lian","Lucas Cheng","Ryan Lee","Javier Sim Juan Neng","Lim Woon Keng Daniel","Bryan Tan"," Romain Maisonnave","Gn Cheng Hng Tusita","Muhammad Zikry B Zainudin","Seet Zhan Yang Bryan","Muhammad Khairul B Mohtar"," Manaol Dwight Eldrich Salvo","Koh Ching Hui","Nigel Diep","Ezra Goh Si Qi","Jeremy Goh Liang Yi","Vincent Chua Yao Sen","Jafred Yeo","Dylan Tay","Leong Weng Jit","Isaiah Boh Yu-teng","Keenan Lim","Daniel Loo Shi Jie","Syed Muhsin B A Azman","Marzuq B Mohamad Rizal Husainy","Ang Kian Siong","Diniy Alfian B Mohammed Shakir","Liang Geng Hui","Benjamin Koh E-terng","Muhammad Ibad B Norhisham","Muhammad Zatri B Abdul Karim","Brandon Quek","Ng Shi Wei Ryan","Jasper Ng Yuze","Luqman Nur Hakim B Rosmani","Minjoot Keith Randall","Tay Soon Lee Eric","Cai Mingjun Alex","Muhammad Farhan Naqiyuddin","Don Tan Yi Wei","Aranas Shawn Valen Dizon","Noor Azim B Abdullah","Otusa Fuminari","Desmond Tee Kai Wen","Muhammad Arif B Alias","Toh Eu Juin","Lin Zhao Zhi","Tan Xian Liang","David Omar Ting Wei Ming","Yeo Yi Shun","Chan Kai Yang","Andrew Michael Walker","Choi Geunmin","Li Jiahao","Muhammad Saifuddin B Johari","Nico Lanz Labating Lim","Brian Ye Zhicheng","Zhang Jianming","Jerry Tan Kin Seng","Mohamed Nihaal S/o Mohamed Z","William Tok Chang Yu","Yong Jung Hahn","Ng Ding Xuan","Er Wen Han","Joel Yuen Wei En","Mah Kim Chuan James","Hu Hua Xin","Seah Kia Luck","Mohamad Riwafiq B Mohamed Noor","Mohammad Sufian B Suhaimi","Muhammad Shakir B Rusli","R Gowtham","Hafizh Syaadi B Katwadi","Lee Zhi Peng Donovan","Mackinmon Casey Daniel","Gil Lim","Bryan Ang Wei Ze","Ho Wen Kai","Summit Bajaj","Goh Jun Yong Gareth","Tan Ling Jen","Ling Cher Er","Xander Anthony","Nathaniel Tan Jun Yang","Rick Peh Zhen Hao","Cha Jia Xun","Chua Chen Wei","Jayson Chua Yu Chong","Amos Wong Wing Tim","Goh Yee Hoo","Muhammad Hazim B Kassim","Thaddeus Chin Yi Yang","Goh Yee Peng","Yu Shen","B Phorkko Rajaa","Danish Ruzall B Aaron Eizaz","Cheong Jung Han","Eng Hong Wei","Brandon","Jonathan Koh","Faris","Martin","Firdaus","Benjamin","Syahin","Wong Wai Fai","Churston Sim E Funn","Muhammad Tajjudin B Mohammad R","Muhammad Khairy B Abdullah","Dawson Wong Jiaqi","Shawn Kerstan Bernard","Muhammad Iqmal B Mahmood","Quek Li Ming","Lee Jun Da","Lim Jun Han Isaac","Hubert Phan Zheng Yu","Tan Wei Hong","Jordan Lee Jun Hao","Lim Jinghan Ray","Melvin Ng Boon Zhong","Ang Jun Jie","Daryl Lim Mingze","Yap Joon Siong","Tan Wei Shuen Lucas","Yeo Zi Jian","Poh Kai Jin","Benjamin Djohan Ting Wei Leong","Kaizen Koh Yu Xiang","Rusell James Foo Junhao","Izz Danial","Saw Smith Htun","Amirul Azri","Dylan Kwok","Danial Sahrani","Muhammad Ya'iisy","Magdaleno Graeshan","Cheow Jun Wen","Ho Yong Kian Gianluca","Gérard Yeo Qi Wei","Ng Zhenqi Jason","James Kua Hong Peng","Tan Teong Yu Owen","Lee Yen Foong Ernest","Benjamin Wee","Yew Zhong Rui","Marcus Ong","Mark Lam","Gerrard Lee","Yap Chee Hong","Chua Yong Yuan Joe","Muhammad Ruzaini B Abdul Razak","Mohammad Irfan B Mohd Haizan","Kurniawan B Mohamed","Boon Zhi Wei","Muhammad Nazri Sanadi","Ng He Chuan","Lai Chun Hong Alan","Riyas Pravin B Jainulabdeen","Jonas Oh How Boon","Ryanud-deen Mohamed Rafeed","Lim Win Tak","Muhammad Harashta B Hasbullah","Muhammad Izzul Islam B Mohd H","Tan Zhen Yu","William Lua Wen Jun","Louis Shia Wei Jie","Ong Chu Hao Trovald","Teo Chai Wei","Jarell Tock Wei-li","Kolanchiyappan Chandru","Muhammad Danial B Dahlan","Vinodkumar S/o John Shankaran","Raihan Amirul Hakim B Hamzah","Theshan S/o Veerasingam","Vestal Tyler James","Koh Swee Sen","Lie Jia Jiang","Emmanuel Ryan Paul","Ryan John Yong Jian Bickham","Azri Zuhairi B Mohamed Azman","Goh Si An","Muhammad Rohaimie B Juraimi","Liong Jun Heng","Randall Yeo Guo Bin","Muhammad Syafiq B Samsudin","Hoo Jun Quan","Nigel Ong Yong Ser","Yiah Zhu Cheng","Ng Zi Yao","Kong Ming Rong Nigel","Pang Jun Rong","Bennett Chong Jun Zhong","William Lee Yao Chen","Yeo Bing Heng","Lim Wei Chen","Norman Chen","Lim Tian Hui","Kannan S/o Kathiresan","Bess Glazzard Tay","Roby Tanama","Benjamin Wong","Brennen Ang","Tan Kai Shun Ashton","Chadalavada Abhijit","Cheong Wei Soon","Liao Ze Li Jacques","Koh Chee Heng Andy","Jethro Sim Sheng Yang","Aaron Chan Siang Joo","Ryton Teo Zhao Xuan","Sim Yong Zheng","Ong Yi Xiang","Lucas Allan Joel Thariyan","Emil Harris B Jefree","Isaac Arul Mahesan","Tay Shao Ren Nigel","Andrew Chan Siang Ee","Ivan Song","Ayden Mohan","Kiranraj","Lee Jung Ha","Jed Elliot Tee Wen Liang","Chin Tze Kai Jonathan","Andre Christian Macaraig Chuabio","Kim Dong Ho","Timothy Toh Wei En","Kim Dong Jun","Juan Banchs","Daiki Tagami","Adam B. Zuhri","Naseer Husein","Ryan Teng Yi Ren","Seow Rui-an","Teo Wen Ming","Mohamed Shaafir S/o Saibudeen","Sebastian Ang Jie","Tay Wei Hao Joshuaa","Mohd Irfan Qabeel","Ierhan Muhd Raushan","Darren Wong","Aaron Shane Tan Wen","Chong Wei Guan","Daniel Abdullah Bin Harun","Muhammad Rayyan Mohamad Rafi Putera","Tan Yong Ming","Ancel Chong Fu Shea","Andrew Lo Zhi Sheng","Teng Chen Chang Gabriel","Lok Yang Teng","Nguyen Minh Tri","Chiam Chuen","Ian Joseph Acu Kang","Dixit Yash Ashok","Chiang Jai Ming Luke","Nehemiah Chai Yang","Heng Sin Tee","T Akash","Ashraf Khan","Madhavan Parthiban","Javier Teo Jie Wei","Frenathan Ng","Lim Jie Han August","Hayden Ang Wei En","Liau Gary","Lim Ekson","Foo Mao Kang","G Sathyan","Marcus Lim Jun Yuan","Tan Jun Khiang Keith","Gabriel James Neo","Jared Ng Yujie","M Shevaraman","Rohin Singh","Leow Yuan","Kum Kai Weng","Chim Kean Weng Shaun","Muhammad Erfyan B Mohamed R","Irsyad B Abdul Aziz","Daeng Ansari B Mohamed Shah","Muhammad Akid Aqil B Kasmawi","Dinesh Palani S/o Mahandran","Yuvanesh S/o Krishnasamy","Shawn Pang Seng Jun","Andrew Lim Wei Lun","Shawn Chia Wen En","Kevin Tang Xin Rong","Kevin Samuel Sumanam","Assiddiq B Surani","Mohamed Hassan S/o Mohamed Y","Justin B Tabiar De La Rosa","Chua Wen Bin Kenny","Joseph Kwee Wen Guang@guo Jia Min","Terry Wong Yok Sheng","Mohammad Syafiy Firdaus B S","Leonard Teo Zhi Min","Chua Shan Wu Darryn","Mohamed Ensaaf S/o Aziz"};
const string TYPE[2] = {"Admin", "Member"};

string vecToStr(string in)
{
	int temp;
	string out, toDE;
	stringstream tmpCharInInt;
	
	string::size_type pos = 0;
	do{
		if(pos == 0)
			tmpCharInInt << in.substr(pos, 3);
		else
			tmpCharInInt << in.substr(pos+1, 3);
		tmpCharInInt >> temp;
		toDE += (char)temp;
		tmpCharInInt.str(string());
		pos = in.find_first_of(',', pos+1);
	}while(pos != string::npos);
	return toDE;
}

int main()
{
	fstream fileEN(FILENAME_EN, ios::out);
	fstream fileDE(FILENAME_DE, ios::out);
	
	srand(time(NULL));
	int pwdLen, pwdChar, name, noOfGra, gra, noOfReq, req, points, type, HIID;
	string pwdDE, pwdEN, pwdENVec;
	string revGranted, revRequested;
	stringstream HI;
	
	for(int i = 0; i < NO_OF_MEMBERS; i++)
	{
		// so they are reset for each member
		int reqAry[MAX_REVIEW], graAry[MAX_REVIEW];
		bool dup = false;
		
		name = i % 364;
		
		srand(rand());
		pwdLen = (rand() % (MAX_PASSWORD_LENGTH - MIN_PASSWORD_LENGTH + 1)) 
						+ MIN_PASSWORD_LENGTH;
		pwdDE = "";
		for(int j = 0; j < pwdLen; j++)
		{
			srand(rand());
			pwdChar = (rand() % 95) + 32;
			pwdDE += (char)pwdChar;
		}
		pwdENVec = "";
		pwdENVec = charStrToIntStr(encrypt(pwdDE));

		srand(rand());
		noOfGra = rand() % MAX_REVIEW + 1;
		revGranted = "";
		for(int j = 0; j < noOfGra; j++)
		{
			// do-while gra has been used
			do{
				srand(rand());
				gra = (rand() % NO_OF_HOTELS + 1) + 1;
				dup = false;
				// check previous gra for each new gra
				for(int g = 0; g < j; g++)
					if(gra == graAry[g])
					{
						dup = true;
						break;
					}
			}while(dup == true);
			dup = false;
			graAry[j] = gra;
			
			HI.str(string());
			HI << "HI" << setw(5) << setfill('0') << gra;
			revGranted += HI.str();
			if(j < noOfGra-1)
				revGranted += ",";
		}
		
		srand(rand());
		noOfReq = rand() % MAX_REVIEW + 1;
		revRequested = "";
		for(int j = 0; j < noOfReq; j++)
		{
			// do-while req has been used			
			do{
				srand(rand());
				req = (rand() % NO_OF_HOTELS + 1) + 1;
				dup = false;
				// check previous req for new req
				for(int r = 0; r < j; r++)
					if(req == reqAry[r])
					{
						dup = true;
						break;
					}
				// check gra for new req
				if(dup == false)
					for(int g = 0; g < noOfGra; g++)
						if(req == graAry[g])
						{
							dup = true;
							break;
						}
			}while(dup == true);
			dup = false;
			reqAry[j] = req;
			
			HI.str(string());
			HI << "HI" << setw(5) << setfill('0') << req;
			revRequested += HI.str();
			if(j < noOfReq-1)
				revRequested += ",";
		}
		
		srand(rand());
		points = (rand() % MAX_POINTS) + MIN_POINTS;
		
		if(i % 4 == 0)
		{
			srand(rand());		
			type = rand() % 2;
		}
		else
			type = 1;
		
	
	fileDE << "ID" << setw(5) << setfill('0') << i+1 << "~"
		 << NAME[name] << "~"
		 << pwdDE << "~";
	if(type == 0)
		fileDE << "~";
	else
		fileDE << revGranted << "~" << revRequested;
	fileDE << "~" << points 
			<< "~" << TYPE[type] 
			<< "\n";
	
	cout << "ori: " << pwdDE << " enc: " << encrypt(pwdDE) << " dec: " << decrypt(encrypt(pwdDE)) << "\n"
			<< "ori str int: " << charStrToIntStr(pwdDE) << " ori str char: " << intStrToCharStr(charStrToIntStr(pwdDE)) << endl;
	
	fileEN << "ID" << setw(5) << setfill('0') << i+1 << "~"
		 << NAME[name] << "~"
		 << pwdENVec << "~";
	if(type == 0)
		fileEN << "~";
	else
		fileEN << revGranted << "~" << revRequested;
	fileEN << "~" << points 
			<< "~" << TYPE[type]
			<< "\n";
	}	
	fileEN.close();
	fileDE.close();
}